package com.sandasai.action.user;
import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sandasai.service.UserManager;
import com.sandasai.vo.UserBean;

@Controller
@Scope("prototype")
public class RegistAction extends ActionSupport {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private UserManager userManager;
	
	private String username;
    private String password;
    private String email;
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    /**
     * @return
     */
    public String execute() {
        UserBean userBean = new UserBean();
        userBean.setUsername(username);
        userBean.setPassword(password);
        userBean.setEmail(email);
        if(this.userManager.checkRegistration(userBean)) {
            ActionContext ctx = ActionContext.getContext();
            ctx.getSession().put("username", username);
            ctx.getSession().put("userType", UserBean.USER_TYPE_STUDENT);
            userManager.sendActivationEmail(username);
            return SUCCESS;
        }else {
            return ERROR;
        }
    }
}